<template>
  <div class="loginPage">
    <div class="loginBanner">
      <img src="../../assets/bg.png" alt="">
    </div>
    <div class="loginArea">
      <div class="loginTitle">登录</div>

      <el-form class="loginForm" :model="form" label-width="120px">
        <el-form-item class="account-ipt">
          <el-input
            size="large"
            v-model="form.account"
            prefix-icon="User"
            placeholder="请输入用户名"
          />
          <el-icon class="clear-btn" @click="handleClear">
            <CircleClose />
          </el-icon>
        </el-form-item>

        <el-form-item class="password-ipt">
          <el-input
            size="large"
            v-model="form.password"
            prefix-icon="Lock"
            :type="form.pwdType"
            placeholder="请输入密码"
            @keyup.enter.native="handleLogin"
          />
          <el-icon
            @click="togglePassword"
            :class="[{ 'show-btn': true }, { 'eye-color': form.showPassword }]"
          >
            <View />
          </el-icon>
        </el-form-item>

        <div class="btn">
          <el-button class="login-btn" @click="handleLogin">登 录</el-button>
          <span class="forget-tip" @click="handleForget">忘记密码</span>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive } from "vue";

import { login } from "@/api/user/index.ts";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";

const form = reactive({
  account: "",
  password: "",
  showPassword: false,
  pwdType: "password",
});

const handleClear = function () {
  form.account = "";
};

const togglePassword = function () {
  form.showPassword = !form.showPassword;
  form.pwdType = form.pwdType === "" ? "password" : "";
};

const router = useRouter();

const handleLogin = async () => {
  const { data: res } = await login({
    userName: form.account,
    userPassword: form.password,
  });

  if (res.code === 200) {
    window.sessionStorage.setItem(
      "token",
      `${res.data.tokenHead} ${res.data.token}`
    );

    window.sessionStorage.setItem('userInfo', JSON.stringify(res.data.userInfo));

    if (res.data.userInfo.role === '管理员') {
      router.push("/organization-manage");
    } else {
      router.push("/organization-member-manage");
    }

    ElMessage({
      message: "登录成功",
      type: "success",
    });
  }
};

const handleForget = () => {
  ElMessage('请联系管理员重置密码')
}
</script>

<style lang="less" scoped>
.loginPage {
  position: relative;
  width: 100vw;
  height: 100vh;

  .loginBanner {
    position: absolute;
    top: 20%;
    right: 48%;
    width: 900px;
    img {
      width: 100%;
    }
  }
  .loginArea {
    position: absolute;
    top: 16%;
    right: 10%;
    width: 500px;
    height: 650px;
    box-sizing: border-box;
    padding: 40px;
    border-radius: 30px;
    box-shadow: 0 6px 20px 0 rgb(0 0 0 / 19%);
    background-color: #fff;
    overflow: hidden;

    .loginTitle {
      font-family: "Impact Normal", "Impact", sans-serif;
      color: #2f5786;
      font-size: 48px;
      margin-top: 50px;
    }
  }
}

.loginForm {
  margin: 48px 0;

  :deep(.el-form-item__content) {
    margin-left: 0 !important;
  }

  :deep(.el-input__wrapper) {
    box-shadow: none;
    border-radius: 0;
    border-bottom: 1px #ccc solid;
    font-size: 20px;
  }

  :deep(.el-input__prefix) {
    font-size: 30px;
  }

  .account-ipt,
  .password-ipt {
    position: relative;
    height: 60px;
    font-size: 16px;
    margin: 56px 0;
  }

  .clear-btn,
  .show-btn {
    position: absolute;
    right: 10px;
    color: #ccc;
    cursor: pointer;
    font-size: 24px;
  }

  .eye-color {
    color: #409eff;
  }

  .btn {
    position: relative;
    .login-btn {
      width: 100%;
      padding: 32px 0;
      margin: 20px 0;
      font-size: 24px;
      border-radius: 33px;
      color: #fff;
      background-color: #94aecf;
    }

    .forget-tip {
      position: absolute;
      bottom: -8px;
      right: 20px;
      cursor: pointer;
      color: #666;
    }
  }
}
</style>
